package com.cysion.study.ui.login

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.CompoundButton
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.utils.ACache
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other._setOnClickListener
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.MainActivity
import com.cysion.study.R
import com.cysion.study.constant.BUNDLE_KEY
import com.cysion.study.constant.LOGIN_IN
import com.cysion.study.entity.UserEntity
import com.cysion.study.event.UserEvent
import com.cysion.study.helper.UserCache
import com.cysion.study.presenter.LoginPresenter
import com.cysion.study.ui.iview.LoginView
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import kotlinx.android.synthetic.main.activity_login.*
import org.greenrobot.eventbus.EventBus

/**
 * Created by chu on 2019-06-29.
 */
class LoginActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener, LoginView, View.OnClickListener {


    val presenter by lazy {
        LoginPresenter().apply {
            attach(this@LoginActivity)
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }


    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))

        cb_teacher.setOnCheckedChangeListener(this)
        cb_private.setOnCheckedChangeListener(this)

//        普通老师 18628178473   123456
//        私教 15690345228     123456

        //15610051732   123456
//        etUsername.setTextEx("18628178473")
        //15552866455
        //显示测试账号
//        etUsername.setTextEx("18513348852")
//        etUsername.setTextEx("13000000001")
//        etPwd.setTextEx("123456")
        tvLogin._setOnClickListener {
            val account = etUsername.text.toString()
            val pwd = etPwd.text.toString()
            etUsername.checkBlank("手机号不能为空") ?: return@_setOnClickListener
            etPwd.checkBlank("密码不能为空") ?: return@_setOnClickListener
            if (etUsername.text.toString().length < 11 || etPwd.text.length < 6) {
                ShowToast("请输入正确的手机号或6位以上密码")
                return@_setOnClickListener
            }
            cb_teacher.isChecked=true
            var type = "1"//登录类型 0.普通员工 1.私教
            if (cb_teacher.isChecked) {
                type = "1"
            } else if (cb_private.isChecked) {
                type = "2"
            }
            ACache.get(Box.context).put("teacherType", type)
            presenter.login(account, pwd, type)//登录接口请求
        }

        tvRegister.setOnClickListener(this)
        tv_forget.setOnClickListener(this)
        tv_agree.setOnClickListener(this)
        tv_zc.setOnClickListener(this)

    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_agree -> {
//                self.startActivity_ex<RegisterActivity>()
                //类型：1、家长端关于介绍；2、教师端关于介绍；3.用户协议；4.隐私政策  0 网页详情
                //类型：1、家长端关于介绍；2、教师端关于介绍 3.家长端用户协议 4.家长端隐私政策 5.普教端用户协议 6.普教端隐私政策
                var bundle = Bundle()
                bundle.putString("type","5")
                UIHelper.jump(self, WebViewActivity::class.java,bundle)
            }
            R.id.tv_zc -> {
                var bundle = Bundle()
                bundle.putString("type","6")
                UIHelper.jump(self, WebViewActivity::class.java,bundle)
            }
            R.id.tvRegister -> {
                UIHelper.jump(self, RegisterActivity::class.java)
            }
            R.id.tv_forget -> {
                //pageType类型:0.注册 1.修改登录密码 2.修改支付密码 3.登陆密码
                // 4.修改手机号码 5、绑定手机号码 6、提现银行卡绑定 7、家教老师H5推广注册
                val pageType = "1"
                var bundle = Bundle()
                bundle.putString("pageType", pageType)
                self.startActivity_ex<ForgetPwdActivity>(BUNDLE_KEY,bundle)
            }
        }
    }


    override fun setUserInfo(u: UserEntity) {
        ShowToast("登录成功")
        ACache.get(Box.context).put("token", u.token)

        CLog.i("token=${UserCache.token}")
        setResult(100)
        finish()
        UIHelper.jump(this,MainActivity::class.java)
        EventBus.getDefault().post(UserEvent(LOGIN_IN, ""))
    }

    override fun loading() {
        Alert.loading(self)
    }

    override fun stopLoad() {
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        ShowToast(msg)
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        if (buttonView != null) {
            if (buttonView.id == cb_teacher.id) {
                cb_private.isChecked = !isChecked
            } else if (buttonView.id == cb_private.id) {
                cb_teacher.isChecked = !isChecked
            }
        }
    }





    override fun closeMvp() {
        presenter.detach()
    }
}